---
title: "Maven Plugin Development"
date: 2019-7-25
categories:
- java
- thymeleaf
- maven
tags:
---

<div id="content">
<div class="org-src-container">
<pre class="src src-java">
<span style="font-style: italic;">/**</span>
<span style="font-style: italic;"> * src: maven properties plugin</span>
<span style="font-style: italic;"> * https://github.com/mojohaus/properties-maven-plugin.git</span>
<span style="font-style: italic;">n */</span>

<span style="font-weight: bold; font-style: italic;">/*</span>
<span style="font-weight: bold; font-style: italic;"> * Licensed to the Apache Software Foundation (ASF) under one</span>
<span style="font-weight: bold; font-style: italic;"> * or more contributor license agreements.  See the NOTICE file</span>
<span style="font-weight: bold; font-style: italic;"> * distributed with this work for additional information</span>
<span style="font-weight: bold; font-style: italic;"> * regarding copyright ownership.  The ASF licenses this file</span>
<span style="font-weight: bold; font-style: italic;"> * to you under the Apache License, Version 2.0 (the</span>
<span style="font-weight: bold; font-style: italic;"> * "License"); you may not use this file except in compliance</span>
<span style="font-weight: bold; font-style: italic;"> * with the License.  You may obtain a copy of the License at</span>
<span style="font-weight: bold; font-style: italic;"> *</span>
<span style="font-weight: bold; font-style: italic;"> *   http://www.apache.org/licenses/LICENSE-2.0</span>
<span style="font-weight: bold; font-style: italic;"> *</span>
<span style="font-weight: bold; font-style: italic;"> * Unless required by applicable law or agreed to in writing,</span>
<span style="font-weight: bold; font-style: italic;"> * software distributed under the License is distributed on an</span>
<span style="font-weight: bold; font-style: italic;"> * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY</span>
<span style="font-weight: bold; font-style: italic;"> * KIND, either express or implied.  See the License for the</span>
<span style="font-weight: bold; font-style: italic;"> * specific language governing permissions and limitations</span>
<span style="font-weight: bold; font-style: italic;"> * under the License.</span>
<span style="font-weight: bold; font-style: italic;"> */</span>

<span style="font-weight: bold;">import</span> <span style="font-weight: bold; text-decoration: underline;">java</span>.<span style="font-weight: bold; text-decoration: underline;">util</span>.<span style="font-weight: bold; text-decoration: underline;">Enumeration</span>;
<span style="font-weight: bold;">import</span> <span style="font-weight: bold; text-decoration: underline;">java</span>.<span style="font-weight: bold; text-decoration: underline;">util</span>.<span style="font-weight: bold; text-decoration: underline;">Properties</span>;

<span style="font-weight: bold;">import</span> <span style="font-weight: bold; text-decoration: underline;">org</span>.<span style="font-weight: bold; text-decoration: underline;">apache</span>.<span style="font-weight: bold; text-decoration: underline;">maven</span>.<span style="font-weight: bold; text-decoration: underline;">plugin</span>.<span style="font-weight: bold; text-decoration: underline;">MojoExecutionException</span>;
<span style="font-weight: bold;">import</span> <span style="font-weight: bold; text-decoration: underline;">org</span>.<span style="font-weight: bold; text-decoration: underline;">apache</span>.<span style="font-weight: bold; text-decoration: underline;">maven</span>.<span style="font-weight: bold; text-decoration: underline;">plugin</span>.<span style="font-weight: bold; text-decoration: underline;">MojoFailureException</span>;
<span style="font-weight: bold;">import</span> <span style="font-weight: bold; text-decoration: underline;">org</span>.<span style="font-weight: bold; text-decoration: underline;">apache</span>.<span style="font-weight: bold; text-decoration: underline;">maven</span>.<span style="font-weight: bold; text-decoration: underline;">plugins</span>.<span style="font-weight: bold; text-decoration: underline;">annotations</span>.<span style="font-weight: bold; text-decoration: underline;">LifecyclePhase</span>;
<span style="font-weight: bold;">import</span> <span style="font-weight: bold; text-decoration: underline;">org</span>.<span style="font-weight: bold; text-decoration: underline;">apache</span>.<span style="font-weight: bold; text-decoration: underline;">maven</span>.<span style="font-weight: bold; text-decoration: underline;">plugins</span>.<span style="font-weight: bold; text-decoration: underline;">annotations</span>.<span style="font-weight: bold; text-decoration: underline;">Mojo</span>;

<span style="font-weight: bold; text-decoration: underline;">@Mojo</span>( name = <span style="font-style: italic;">"write-project-properties"</span>, defaultPhase = <span style="font-weight: bold; text-decoration: underline;">LifecyclePhase</span>.NONE, threadSafe = <span style="font-weight: bold; text-decoration: underline;">true</span> )
<span style="font-weight: bold;">public</span> <span style="font-weight: bold;">class</span> <span style="font-weight: bold; text-decoration: underline;">WriteProjectProperties</span>
    <span style="font-weight: bold;">extends</span> <span style="font-weight: bold; text-decoration: underline;">AbstractWritePropertiesMojo</span>
{

    <span style="font-style: italic;">/** </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">{@inheritDoc}</span><span style="font-style: italic;"> */</span>
    <span style="font-weight: bold;">public</span> <span style="font-weight: bold; text-decoration: underline;">void</span> <span style="font-weight: bold;">execute</span>()
        <span style="font-weight: bold;">throws</span> <span style="font-weight: bold; text-decoration: underline;">MojoExecutionException</span>, <span style="font-weight: bold; text-decoration: underline;">MojoFailureException</span>
    {
        validateOutputFile();
        <span style="font-weight: bold; text-decoration: underline;">Properties</span> <span style="font-weight: bold; font-style: italic;">projProperties</span> = <span style="font-weight: bold;">new</span> <span style="font-weight: bold; text-decoration: underline;">Properties</span>();
        projProperties.putAll( getProject().getProperties() );

        <span style="font-weight: bold; text-decoration: underline;">Properties</span> <span style="font-weight: bold; font-style: italic;">systemProperties</span> = System.getProperties();

        <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">allow system properties to over write key/value found in maven properties</span>
        <span style="font-weight: bold; text-decoration: underline;">Enumeration</span>&lt;?&gt; <span style="font-weight: bold; font-style: italic;">enumeration</span> = systemProperties.keys();
        <span style="font-weight: bold;">while</span> ( enumeration.hasMoreElements() )
            {
                <span style="font-weight: bold; text-decoration: underline;">String</span> <span style="font-weight: bold; font-style: italic;">key</span> = (<span style="font-weight: bold; text-decoration: underline;">String</span>) enumeration.nextElement();
                <span style="font-weight: bold; text-decoration: underline;">String</span> <span style="font-weight: bold; font-style: italic;">value</span> = systemProperties.getProperty( key );
                <span style="font-weight: bold;">if</span> ( projProperties.get( key ) != <span style="font-weight: bold; text-decoration: underline;">null</span> )
                    {
                        projProperties.put( key, value );
                    }

            }

        projProperties.put(<span style="font-style: italic;">"projectArtifactId"</span>, getProject().getArtifactId());
        projProperties.put(<span style="font-style: italic;">"projectVersion"</span>, getProject().getVersion());

        writeProperties( projProperties, getOutputFile() );

    }
}

<span style="font-weight: bold;">public</span> <span style="font-weight: bold;">abstract</span> <span style="font-weight: bold;">class</span> <span style="font-weight: bold; text-decoration: underline;">AbstractWritePropertiesMojo</span>
    <span style="font-weight: bold;">extends</span> <span style="font-weight: bold; text-decoration: underline;">AbstractMojo</span>
{

    <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">注入的参数，这个对象中有maven项目的各种信息</span>
    <span style="font-weight: bold; text-decoration: underline;">@Parameter</span>( defaultValue = <span style="font-style: italic;">"${project}"</span>, required = <span style="font-weight: bold; text-decoration: underline;">true</span>, readonly = <span style="font-weight: bold; text-decoration: underline;">true</span> )
    <span style="font-weight: bold;">private</span> <span style="font-weight: bold; text-decoration: underline;">MavenProject</span> <span style="font-weight: bold; font-style: italic;">project</span>;

    <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">pom.xml 中配置的参数</span>
    <span style="font-weight: bold; text-decoration: underline;">@Parameter</span>( required = <span style="font-weight: bold; text-decoration: underline;">true</span> )
    <span style="font-weight: bold;">private</span> <span style="font-weight: bold; text-decoration: underline;">File</span> <span style="font-weight: bold; font-style: italic;">outputFile</span>;

    <span style="font-style: italic;">/**</span>
<span style="font-style: italic;">     * </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">@param</span><span style="font-style: italic;"> properties </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">{@link Properties}</span>
<span style="font-style: italic;">     * </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">@param</span><span style="font-style: italic;"> file </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">{@link File}</span>
<span style="font-style: italic;">     * </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">@throws</span><span style="font-style: italic;"> MojoExecutionException </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">{@link MojoExecutionException}</span>
<span style="font-style: italic;">     */</span>
    <span style="font-weight: bold;">protected</span> <span style="font-weight: bold; text-decoration: underline;">void</span> <span style="font-weight: bold;">writeProperties</span>(<span style="font-weight: bold; text-decoration: underline;">Properties</span> <span style="font-weight: bold; font-style: italic;">properties</span>, <span style="font-weight: bold; text-decoration: underline;">File</span> <span style="font-weight: bold; font-style: italic;">file</span> )
        <span style="font-weight: bold;">throws</span> <span style="font-weight: bold; text-decoration: underline;">MojoExecutionException</span>
    {
        <span style="font-weight: bold; text-decoration: underline;">FileOutputStream</span> <span style="font-weight: bold; font-style: italic;">fos</span> = <span style="font-weight: bold; text-decoration: underline;">null</span>;
        <span style="font-weight: bold;">try</span>
            {
                fos = <span style="font-weight: bold;">new</span> <span style="font-weight: bold; text-decoration: underline;">FileOutputStream</span>( file );
                properties.store( fos, <span style="font-style: italic;">"Properties"</span> );
            }
        <span style="font-weight: bold;">catch</span> ( <span style="font-weight: bold; text-decoration: underline;">FileNotFoundException</span> <span style="font-weight: bold; font-style: italic;">e</span> )
            {
                getLog().error( <span style="font-style: italic;">"Could not create FileOutputStream: "</span> + fos );
                <span style="font-weight: bold;">throw</span> <span style="font-weight: bold;">new</span> <span style="font-weight: bold; text-decoration: underline;">MojoExecutionException</span>( e.getMessage(), e );
            }
        <span style="font-weight: bold;">catch</span> ( <span style="font-weight: bold; text-decoration: underline;">IOException</span> <span style="font-weight: bold; font-style: italic;">e</span> )
            {
                getLog().error( <span style="font-style: italic;">"Error writing properties: "</span> + fos );
                <span style="font-weight: bold;">throw</span> <span style="font-weight: bold;">new</span> <span style="font-weight: bold; text-decoration: underline;">MojoExecutionException</span>( e.getMessage(), e );
            }

        <span style="font-weight: bold;">try</span>
            {
                fos.close();
            }
        <span style="font-weight: bold;">catch</span> ( <span style="font-weight: bold; text-decoration: underline;">IOException</span> <span style="font-weight: bold; font-style: italic;">e</span> )
            {
                getLog().error( <span style="font-style: italic;">"Error closing FileOutputStream: "</span> + fos );
                <span style="font-weight: bold;">throw</span> <span style="font-weight: bold;">new</span> <span style="font-weight: bold; text-decoration: underline;">MojoExecutionException</span>( e.getMessage(), e );
            }
    }

    <span style="font-style: italic;">/**</span>
<span style="font-style: italic;">     * </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">@throws</span><span style="font-style: italic;"> MojoExecutionException </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">{@link MojoExecutionException}</span>
<span style="font-style: italic;">     */</span>
    <span style="font-weight: bold;">protected</span> <span style="font-weight: bold; text-decoration: underline;">void</span> <span style="font-weight: bold;">validateOutputFile</span>()
        <span style="font-weight: bold;">throws</span> <span style="font-weight: bold; text-decoration: underline;">MojoExecutionException</span>
    {
        <span style="font-weight: bold;">if</span> ( outputFile.isDirectory() )
        {
            <span style="font-weight: bold;">throw</span> <span style="font-weight: bold;">new</span> <span style="font-weight: bold; text-decoration: underline;">MojoExecutionException</span>( <span style="font-style: italic;">"outputFile must be a file and not a directory"</span> );
        }
        <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">ensure path exists</span>
        <span style="font-weight: bold;">if</span> ( outputFile.getParentFile() != <span style="font-weight: bold; text-decoration: underline;">null</span> )
        {
            outputFile.getParentFile().mkdirs();
        }
    }

    <span style="font-style: italic;">/**</span>
<span style="font-style: italic;">     * </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">@return</span><span style="font-style: italic;"> </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">{@link MavenProject}</span>
<span style="font-style: italic;">     */</span>
    <span style="font-weight: bold;">public</span> <span style="font-weight: bold; text-decoration: underline;">MavenProject</span> <span style="font-weight: bold;">getProject</span>()
    {
        <span style="font-weight: bold;">return</span> project;
    }

    <span style="font-style: italic;">/**</span>
<span style="font-style: italic;">     * </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">@return</span><span style="font-style: italic;"> </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">{@link #outputFile}</span>
<span style="font-style: italic;">     */</span>
    <span style="font-weight: bold;">public</span> <span style="font-weight: bold; text-decoration: underline;">File</span> <span style="font-weight: bold;">getOutputFile</span>()
    {
        <span style="font-weight: bold;">return</span> outputFile;
    }

}
</pre>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2019-7-25</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
